package kr.co.happenstance;

import java.util.ArrayList;

import kr.co.happenstance.adapter.PayLogAdapter;
import kr.co.happenstance.lib.PaylogLib;
import kr.co.happenstance.lib.vo.LibResultVo;
import kr.co.happenstance.lib.vo.PayLogResultVo;
import kr.co.happenstance.lib.vo.PayLogResultVo.PayLog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;

public class PaylogActivity extends BaseActivity implements View.OnClickListener, OnScrollListener{
	protected final String TAG = "<<<PaylogActivity>>>";
	
	/**
	 * 현재 페이지
	 */
	private int currentPage;
	
	/**
	 * 총페이지
	 */
	private int totalPage;
	
	/**
	 * ListView Adapter 객체
	 */
	private PayLogAdapter payLogAdapter; 
	
	/**
	 * Adapter ArrayList객체
	 */
	private ArrayList<PayLog> payLogList;
	
	/**
	 * ListView 객체
	 */
	private ListView lvPayLogList;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_paylog);
		
		lvPayLogList = (ListView) this.findViewById(R.id.lv_paylog_list);
		
		
		payLogList = new ArrayList<PayLog>();
		payLogAdapter =  new PayLogAdapter(this, payLogList);
		lvPayLogList.setAdapter(payLogAdapter);
		lvPayLogList.setOnScrollListener(this);
		
		currentPage=1;
		new PaylogTask().execute();
		
		addActivity(this);
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
			case R.id.btn_pop_close:
				Log.d(TAG, "btn_pop_close onClick.....");
				finish();
				break;
		}
		
	}	
	
	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		//현재 가장 처음에 보이는 셀번호와 보여지는 셀번호를 더한값이 전체의 숫자와 동일해지면 가장 아래로 스크롤 되었다고 가정합니다.		
		int count = totalItemCount - visibleItemCount;
		if(firstVisibleItem >= count && totalItemCount != 0){
			
			currentPage++;
			if(currentPage <= totalPage){
				Log.d(TAG, "Loading next items");
				new PaylogTask().execute();	
			}
		}		
		
		
	}	
	
	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub
	}


	
	
	
	private class PaylogTask extends AsyncTask<Void, Void, LibResultVo> {
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			showProgress();
		}	
		
		@Override
		protected LibResultVo doInBackground(Void... arg0) {
			LibResultVo vo = null;
			try{
				PaylogLib lib = new PaylogLib();
				vo = lib.list(myApp.id, currentPage);
			}catch(Exception e){
				e.printStackTrace();
			}
			return vo;
		}
		
		@Override
		protected void onPostExecute(LibResultVo resultVo) {
			closeProgress();
			
			PayLogResultVo vo = (PayLogResultVo) resultVo;
			if(vo == null){
				Log.d(TAG, "PayLogResultVo null......");
				new CustomAlertDialogBuilder(PaylogActivity.this, R.string.alert_title, R.string.alert_msg_exception).create();
			}else{
				totalPage = vo.getTotalPage();
				Log.d(TAG, "totalPage["+totalPage+"]");
				if(totalPage > 0){
					ArrayList<PayLog> tmpPayLogList = vo.getPayLogList();
					for(PayLog v: tmpPayLogList){
						payLogList.add(v);
					}
					payLogAdapter.notifyDataSetChanged();
				}
				
				Log.d(TAG, "list.size()["+payLogList.size()+"]");
			}
			
		}	
		
	}

	
	
}
